package com.highcom.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.highcom.config.RedisConfig;
import com.highcom.constant.RedisConstant;
import com.highcom.entity.PageResult;
import com.highcom.entity.QueryPageBean;
import com.highcom.mapper.*;
import com.highcom.model.*;
import com.highcom.service.SetmealService;
import freemarker.core.ParseException;
import freemarker.template.Configuration;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.Template;
import freemarker.template.TemplateNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImpl  implements SetmealService {

    @Autowired
    private  TSetmealMapper tSetmealMapper;

    @Autowired
    private TSetmealCheckgroupMapper tSetmealCheckGroupMapper;

    @Autowired
    private TCheckgroupMapper tCheckGroupMapper;

    @Autowired
    private TCheckgroupCheckitemMapper tCheckgroupCheckitemMapper;

    @Autowired
    private TCheckitemMapper tCheckitemMapper;

    private static JedisPool jedisPool;


    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    private static String outPutPath = "/Users/dark/TEACH/0516林大奥林22计-springboot(HCare)-ai/HCare-boot/src/main/resources/static/pages/mobiles";
    @Autowired
    private RedisConfig redisConfig;

    @PostConstruct
    public void init(){
        jedisPool = redisConfig.redisPoolFactory();
    }


    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> pages = tSetmealMapper.findPage(queryString);
        return new PageResult(pages.getTotal(),pages.getResult());

    }

    @Override
    public void add(TSetmeal tSetmeal, Integer[] checkgroupIds) {
        tSetmealMapper.insertSelective(tSetmeal);

        // 上传
        String img = tSetmeal.getImg();// 表单 path

        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,img);
        Integer id = tSetmeal.getId();

        for(Integer checkgroupId : checkgroupIds){
            TSetmealCheckgroupKey tSetmealCheckgroupKey = new TSetmealCheckgroupKey();
            tSetmealCheckgroupKey.setSetmealId(id);
            tSetmealCheckgroupKey.setCheckgroupId(checkgroupId);
            tSetmealCheckGroupMapper.insertSelective(tSetmealCheckgroupKey);
        }
        // 为html做模版数据缓存处理
        generateMobileStaticHtml();
    }

    public void generateMobileStaticHtml(){
        List<TSetmeal> all = this.findAll();
        generateMobileSetmealListHtml(all);
        generateMobileSetmealDetailHtml(all);
    }

    public void generateMobileSetmealListHtml(List<TSetmeal> setmealList) {
            HashMap<Object,Object> dataMap = new HashMap<>();
            dataMap.put("setmealList",setmealList);
            // Stream
            this.generateHtml("mobile_setmeal.ftl","m_setmeal.html",dataMap);
    }


    public void generateMobileSetmealDetailHtml(List<TSetmeal> setmealList) {
        for(TSetmeal tSetmeal : setmealList){
            HashMap<Object,Object> dataMap = new HashMap<>();
            dataMap.put("setmeal" , findById(tSetmeal.getId()));
            // Stream
            this.generateHtml("mobile_setmeal_detail.ftl","m_setmeal_detail"+ tSetmeal.getId() +".html",dataMap);
        }
    }

    public void generateHtml(String templateName,String htmlPageName,Map map){
        Configuration configuration = freeMarkerConfigurer.getConfiguration();
        configuration.setClassicCompatible(true);// ?

        Writer out = null;

        try {
            Template template = configuration.getTemplate(templateName);
            File dockFile = new File(outPutPath + "/" + htmlPageName);
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dockFile)));
            template.process(map, out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }






    @Override
    public List<TSetmeal> findAll() {
        TSetmealExample tsetmealExample = new TSetmealExample();
        List<TSetmeal> tSetmeals = tSetmealMapper.selectByExample(tsetmealExample);
        return tSetmeals;
    }

    @Override
    public TSetmeal findById(int id) {

        TSetmeal tSetmeal = tSetmealMapper.selectByPrimaryKey(id);

        TSetmealCheckgroupExample tSetmealCheckgroupExample = new TSetmealCheckgroupExample();
        tSetmealCheckgroupExample.createCriteria().andSetmealIdEqualTo(id);
        List<TSetmealCheckgroupKey> tSetmealCheckgroupKeys = tSetmealCheckGroupMapper.selectByExample(tSetmealCheckgroupExample);

        ArrayList<TCheckgroup> checkgroups = new ArrayList<>();

        for( TSetmealCheckgroupKey tSetmealCheckgroupKey :tSetmealCheckgroupKeys){
            TCheckgroup tCheckgroup = tCheckGroupMapper.selectByPrimaryKey(tSetmealCheckgroupKey.getSetmealId());
            checkgroups.add(tCheckgroup);
        }

        for(TCheckgroup tCheckgroup : checkgroups){
            ArrayList<TCheckitem> checkitems = new ArrayList<>();
            TCheckgroupCheckitemExample tCheckgroupCheckitemExample = new TCheckgroupCheckitemExample();
            tCheckgroupCheckitemExample.createCriteria().andCheckgroupIdEqualTo(tCheckgroup.getId());
            List<TCheckgroupCheckitemKey> tCheckgroupCheckitemKeys = tCheckgroupCheckitemMapper.selectByExample(tCheckgroupCheckitemExample);
            for(TCheckgroupCheckitemKey tCheckgroupCheckitemKey  :tCheckgroupCheckitemKeys){
                TCheckitem tCheckitem = tCheckitemMapper.selectByPrimaryKey(tCheckgroupCheckitemKey.getCheckitemId());
                checkitems.add(tCheckitem);
            }
            tCheckgroup.setCheckItems(checkitems);
        }

        tSetmeal.setCheckgroups(checkgroups);
        return tSetmeal;
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return List.of();
    }
}














